package lastFMQuery;

import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.Iterator;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;


public class LastFMSearch {
	
	public LastFMSearch(){}

	public Iterator<Event> getQueryResults(String query) {
		Iterator<Event> it = null;
		try {
			
			URI uri = new URI("http",
					"ws.audioscrobbler.com",
					"/2.0/",
					"method=artist.getevents&artist=" + query + "&api_key=b25b959554ed76058ac220b7b2e0a026",
					null);

			URL url = uri.toURL();
			InputStream is = url.openStream();

			JAXBContext jc = JAXBContext.newInstance( "lastFMQuery" );
			Unmarshaller u = jc.createUnmarshaller();

			Lfm results = (Lfm)u.unmarshal( is );
	        
	        it = results.getEvents().getEvent().iterator();
	        
//	        while( it.hasNext()) {
//	        	Artist a = it.next();
//	        	System.out.println( a.getName() + " -> " + a.getUrl());
//	        }
		} catch( Exception e) {
			e.printStackTrace();
		}

		return it;
	}

}
